package MODULES.MOD_XML.VIEWS;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import MODULES.MOD_XML.CONTROLLERS.XMLControllers;

public class XMLViews02 extends JFrame {

    private final JPanel contentPane;
    private final JTable JtbLoadData;
    private final JScrollPane scrollPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    XMLViews02 frame = new XMLViews02();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public XMLViews02() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 722, 399);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnDocFiles = new JButton("Xem dữ liệu từ file XML");
        btnDocFiles.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // 1. Sử dụng Components để hiển cửa sổ lưu files
                JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                String[] EXTENSION = new String[] { "xml" };
                FileNameExtensionFilter filter = new FileNameExtensionFilter("XML files", EXTENSION);
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                int returnVal = objFile.showOpenDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    ParentPath = objFile.getSelectedFile().getParent();
                    FileName = objFile.getSelectedFile().getName();

                    // 2. Khai báo & khởi tạo Controllers
                    XMLControllers objController = new XMLControllers();

                    // 3. Ủy quyền thực thi nghiệp vụ
                    try {
                        String pathFile = ParentPath + "/" + FileName;
                        JtbLoadData.setModel(objController.ConvertListIntoTable(pathFile));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        btnDocFiles.setForeground(new Color(255, 255, 255));
        btnDocFiles.setBackground(new Color(30, 144, 255));
        btnDocFiles.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnDocFiles.setBounds(0, 0, 226, 29);
        contentPane.add(btnDocFiles);

        scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 40, 686, 310);
        contentPane.add(scrollPane);

        JtbLoadData = new JTable();
        scrollPane.setViewportView(JtbLoadData);
    }
}
